package com.xx.bysj.controller;

import com.xx.bysj.aop.LogAop;
import com.xx.bysj.aop.LogConsts;
import com.xx.bysj.dto.HnInformation;
import com.xx.bysj.dto.HnRole;
import com.xx.bysj.service.RoleService;
import com.xx.bysj.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 查询角色信息
     * @param roleName
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/query")
    @ResponseBody
    @LogAop(actionName = "查询角色信息",actionType = LogConsts.ACTION_QUERY)
    public Result query(String roleName,
                        @RequestParam int page,
                        @RequestParam int limit){
        HnRole role = new HnRole();
        role.setRoleName(roleName);
        return roleService.query(role,page,limit);
    }

    /**
     * 获取权限信息
     *
     * @return
     */
    @GetMapping("/menuTree")
    @ResponseBody
    @LogAop(actionName = "查看权限信息",actionType = LogConsts.ACTION_QUERY)
    public Map<String, Object> menuTree(Integer roleId) {
        Map<String, Object> menuTreeMap = roleService.getMenuTreeMap(roleId);
        return menuTreeMap;
    }

    @PostMapping("/addRole")
    @ResponseBody
    @LogAop(actionName = "角色注册",actionType = LogConsts.ACTION_ADD)
    public Result addRole (Authentication authentication, @RequestBody HnRole role){
        return roleService.addRole(authentication,role);
    }

    @PostMapping("/updateRole")
    @ResponseBody
    @LogAop(actionName = "角色注册",actionType = LogConsts.ACTION_UPDATE)
    public Result updateRole (Authentication authentication, @RequestBody HnRole role){
        return roleService.updateRole(authentication,role);
    }

    @PostMapping("/deleteRole")
    @ResponseBody
    @LogAop(actionName = "角色删除",actionType = LogConsts.ACTION_DELETE)
    public Result deleteRole (Authentication authentication, @RequestParam Integer roleId){
        return roleService.deleteRole(roleId);
    }

    @RequestMapping("/queryRole")
    @ResponseBody
    @LogAop(actionName = "查询角色信息",actionType = LogConsts.ACTION_QUERY)
    public Result queryRole(String roleName){
        HnRole role = new HnRole();
        role.setRoleName(roleName);
        return roleService.queryRole(role);
    }
}
